﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Applee.FractCommon;

namespace Applee.DurerFract
{
    public class DurerFract : BaseFract
    {
        //private void GeneratePentagons(PointF point, RandomColor color, float fLength, int iterations)
        //{
        //    if (iterations < 0)
        //        return;

        //    //PointF point0 = new PointF(0, 0);
        //    PointF point0 = point;
        //    DurerPentagon pentagon = new DurerPentagon();
        //    pentagon.MakeShape(point0, fLength);

        //    //PointF[] newPoints = new PointF[6];
        //    //newPoints[0] = pentagon.Points[0]; 
        //    //newPoints[1] = pentagon.Points[1]; 
        //    //newPoints[2] = pentagon.Points[2]; 
        //    //newPoints[3] = pentagon.Points[3];
        //    //newPoints[4] = pentagon.Points[4]; 
        //    //newPoints[5] = pentagon.Points[5];

        //    //Move
        //    //pentagon.MovePentagon(point);

        //    pentagon.Color = color.GetColor(iterations);

        //    //add
        //    mDurerPentagons.Add(pentagon);

        //    float fRatio = NewLengthRatio();

        //    //sin36 = R / (L / 2)
        //    float fNewLength = fRatio * fLength;

        //    float fTempLength = (1 - fRatio) * fLength; //新五边形中心点集是这个边长的五边形的顶点集合


        //    //R = L / Cos54 * 0.5
        //    //float fR = fLength / (float)Math.Cos(54.0 * Math.PI / 180.0) * 0.5F;

        //    //
        //    //float fNewR = Math.Abs(fR * (1 - fRatio));

        //    DurerPentagon pentagonTemp = new DurerPentagon();
        //    pentagonTemp.MakeShape(point0, fTempLength);
        //    //pentagonTemp.LineColor = color.GetColor(iterations);
        //    //mDurerPentagons.Add(pentagonTemp);

        //    //(0, R)
        //    PointF point00 = pentagonTemp.Points[0] ;
        //    GeneratePentagons(point00, color, fNewLength, iterations - 1);

        //    //(R*cos18, R*sin18)
        //    PointF point01 = pentagonTemp.Points[1];
        //    GeneratePentagons(point01, color, fNewLength, iterations - 1);

        //    //(R*sin36, -R*cos36)
        //    PointF point02 = pentagonTemp.Points[2];
        //    GeneratePentagons(point02, color, fNewLength, iterations - 1);

        //    //(-R*sin36, -R*cos36)
        //    PointF point03 = pentagonTemp.Points[3];
        //    GeneratePentagons(point03, color, fNewLength, iterations - 1);

        //    //(-R*cos18, R*sin18)
        //    PointF point04 = pentagonTemp.Points[4];
        //    GeneratePentagons(point04, color, fNewLength, iterations - 1);
        //}

        //#region IFractable 成员

        //private void CalcLength()
        //{
        //    float lctAngle = (float)(18.0F * Math.PI / 180);
        //    float lcbAngle = (float)(36.0F * Math.PI / 180);

        //    SizeF shapeSize = new SizeF((float)(Math.Sin(lcbAngle) / Math.Cos(lctAngle)),
        //        (float)((Math.Sin(lcbAngle) * 2) / (Math.Cos(lcbAngle) + 1)));

        //    float flength = 0;
        //    if (MathUtils.CalcAdaptiveLength(mViewSize, shapeSize, ref flength))
        //    {
        //        //width
        //        //2Rcos18=width
        //        //R=L/(sin36x2)
        //        //L=sin36/cos18xwidth
        //        mLength = (float)(Math.Sin(lcbAngle) / Math.Cos(lctAngle) * flength);
        //    }
        //    else
        //    {
        //        //Height
        //        //Rcos36+R = height
        //        //R=L/(sin36x2)
        //        //L=heightx(sin36x2)/(cos36+1)
        //        mLength = (float)((Math.Sin(lcbAngle) * 2) / (Math.Cos(lcbAngle) + 1) * flength);
        //    }
        //}

        //#endregion

        //#region IDrawable 成员

        ////public void OnPaint(System.Windows.Forms.PaintEventArgs pe)
        ////{
        ////    Rectangle rect = pe.ClipRectangle;
        ////    PointF pointCenter = new PointF((rect.Right + rect.Left) * 0.5F, (rect.Bottom + rect.Top) * 0.5F);

        ////    foreach (DurerPentagon pentagon in mDurerPentagons)
        ////    {
        ////        //pentagon.MovePentagon(pointCenter);
        ////        DurerPentagon pentagonTemp = new DurerPentagon(pentagon);
        ////        pentagonTemp.MovePentagon(new PointF(0, 0), pointCenter);
        ////        pentagonTemp.OnPaint(pe);
        ////    }


        ////    ////pentagon.MovePentagon(pointCenter);
        ////    //DurerPentagon pentagonTemp = new DurerPentagon(mDurerPentagons[50]);
        ////    //pentagonTemp.MovePentagon(pointCenter);
        ////    //pentagonTemp.OnPaint(pe);
        ////}

        //#endregion


        protected override void Initialize()
        {
            float lctAngle = (float)(18.0F * Math.PI / 180);
            float lcbAngle = (float)(36.0F * Math.PI / 180);

            SizeF shapeSize = new SizeF((float)(Math.Sin(lcbAngle) / Math.Cos(lctAngle)),
                (float)((Math.Sin(lcbAngle) * 2) / (Math.Cos(lcbAngle) + 1)));

            float flength = 0;
            if (MathUtils.CalcAdaptiveLength(mViewSize, shapeSize, ref flength))
            {
                //width
                //2Rcos18=width
                //R=L/(sin36x2)
                //L=sin36/cos18xwidth
                mLength = (float)(Math.Sin(lcbAngle) / Math.Cos(lctAngle) * flength);
            }
            else
            {
                //Height
                //Rcos36+R = height
                //R=L/(sin36x2)
                //L=heightx(sin36x2)/(cos36+1)
                mLength = (float)((Math.Sin(lcbAngle) * 2) / (Math.Cos(lcbAngle) + 1) * flength);
            }
        }

        protected override void CreateFract(PointF point, RandomColor color, float fLength, int iterations)
        {
            if (iterations < 0)
                return;

            //PointF point0 = new PointF(0, 0);
            PointF point0 = point;
            DurerPentagon pentagon = new DurerPentagon();
            pentagon.MakeShape(point0, fLength);

            //PointF[] newPoints = new PointF[6];
            //newPoints[0] = pentagon.Points[0]; 
            //newPoints[1] = pentagon.Points[1]; 
            //newPoints[2] = pentagon.Points[2]; 
            //newPoints[3] = pentagon.Points[3];
            //newPoints[4] = pentagon.Points[4]; 
            //newPoints[5] = pentagon.Points[5];

            //Move
            //pentagon.MovePentagon(point);

            pentagon.Color = color.GetColor(iterations);

            //add
            mFractObjects.Add(pentagon);

            float fRatio = NewLengthRatio();

            //sin36 = R / (L / 2)
            float fNewLength = fRatio * fLength;

            float fTempLength = (1 - fRatio) * fLength; //新五边形中心点集是这个边长的五边形的顶点集合


            //R = L / Cos54 * 0.5
            //float fR = fLength / (float)Math.Cos(54.0 * Math.PI / 180.0) * 0.5F;

            //
            //float fNewR = Math.Abs(fR * (1 - fRatio));

            DurerPentagon pentagonTemp = new DurerPentagon();
            pentagonTemp.MakeShape(point0, fTempLength);
            //pentagonTemp.LineColor = color.GetColor(iterations);
            //mDurerPentagons.Add(pentagonTemp);

            //(0, R)
            PointF point00 = pentagonTemp.Points[0];
            CreateFract(point00, color, fNewLength, iterations - 1);

            //(R*cos18, R*sin18)
            PointF point01 = pentagonTemp.Points[1];
            CreateFract(point01, color, fNewLength, iterations - 1);

            //(R*sin36, -R*cos36)
            PointF point02 = pentagonTemp.Points[2];
            CreateFract(point02, color, fNewLength, iterations - 1);

            //(-R*sin36, -R*cos36)
            PointF point03 = pentagonTemp.Points[3];
            CreateFract(point03, color, fNewLength, iterations - 1);

            //(-R*cos18, R*sin18)
            PointF point04 = pentagonTemp.Points[4];
            CreateFract(point04, color, fNewLength, iterations - 1);
        }

        private float NewLengthRatio()
        {
            return (3.0F - (float)Math.Sqrt(5.0F)) * 0.5F;
        }
    }
}
